package com.liuzh.kaoyanspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liuzh.kaoyanspringboot.common.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuzh.kaoyanspringboot.entity.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.liuzh.kaoyanspringboot.service.DictService;
import com.liuzh.kaoyanspringboot.entity.Dict;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘志鸿
 * @since 2023-05-19
 */
@RestController
@RequestMapping("/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    // 新增或更新
    @PostMapping
    public Result save(@RequestBody Dict dict) {
        return Result.success(dictService.saveOrUpdate(dict));
    }

    // 根据 id 删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(dictService.removeById(id));
    }

    // 批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(dictService.removeBatchByIds(ids));
    }

    //查询所有数据
    @GetMapping
    public Result findAll() {
        return Result.success(dictService.list());
    }

    // 根据 id 查询
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(dictService.getById(id));
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @param type
     * @return
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String name,
                           @RequestParam(defaultValue = "") String type) {
        IPage<Dict> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        if(!"".equals(name)){
            queryWrapper.like("name",name);
        }
        if(!"".equals(type)){
            queryWrapper.like("type",type);
        }

//        System.out.println(subjectService.page(page, queryWrapper));
        return Result.success(dictService.page(page, queryWrapper));
    }

}

